<template>
 
    <el-container class="home_container">
       
  <el-header>
    <div class="logo-box">
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>
      
    </div>
     <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <el-container>
    
    <el-aside :width="collapsefalg?64+'px':200+'px'">
      <div class="shuxian" @click="toggleCollapse">|||</div>
       <el-menu
  
       :default-active="activeindex"
       :collapse="collapsefalg"
       :collapse-transition='false'
       unique-opened
      background-color="#333744"
      text-color="#fff"
      router
      active-text-color="#ff3040">
      <!-- 一级菜单 -->
      <el-submenu  :index="item.id+''"  v-for="item in menuList" :key="item.id">
        <!-- 一级菜单模板 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="iconsList[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
          <el-menu-item  @click="toggleindex('/'+item2.path)" :index="'/'+item2.path" v-for="item2 in item.children" :key="item2.id">
         <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{item2.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {

  data () {
    return {
collapsefalg:false,
// 接收左侧导航栏数据
      menuList:[],
      // 左侧的小图标
      iconsList:{
	'125':'iconfont icon-users',
	'103':'iconfont icon-tijikongjian',
	'101':'iconfont icon-shangpin',
	'102':'iconfont icon-danju',
	'145':'iconfont icon-baobiao'
},
   activeindex:''

    }
  },

  methods: {
    // 退出登录操作
      logout(){
          this.$router.push('/login');
          sessionStorage.removeItem('token');
          this.$mg.success('亲亲,退出成功哦')
      },
      // 获取左侧导航栏数据
      async getmenus(){
       const res = await this.axios.get('menus')
          if(res.meta.status !==200) return this.$mg.error(res.meta.msg)
            this.menuList= res.data;
            
      },
      toggleCollapse(){
        this.collapsefalg=!this.collapsefalg
      },
      // 点击高亮
     toggleindex(index){
       this.activeindex = index;
      //  将获取的值存放在本地储存里面
       sessionStorage.setItem('activeindex',index)
     }
  },

  created () {
    this.getmenus()
    // 页面刷新时  将从本地存储的数据赋值给数据  保持高亮
     const index = sessionStorage.getItem('activeindex');
     this.activeindex = index
  }
}
</script>

<style lang='less' scoped>
.home_container{
  height: 100%;
 
}
.el-header {
    background-color: #373d41;
    color: #fff;
     padding-left:0;

     display: flex;
     justify-content: space-between;
       align-items: center;
     .logo-box{
         display:flex;
  align-items: center;
  img{
  width: 50px;
  height: 50px;
  margin-right: 10px;
}
span{
  font-size: 22px;
}
     }
     .el-button{
       width: 70px;
       height: 40px;
     }

  }
  
  .el-aside {
    background-color:#333744;
    text-align: center;
    line-height: 200px;
    height: 100%;
     .shuxian{
    height: 25px;
    background-color: yellowgreen;
    font-size: 18px;
    letter-spacing: 3px;
    text-align: center;
    line-height: 25px;
    cursor: pointer;
    user-select: none;
  }
  }
  
  .el-main {
    background-color: #E9EEF3;
   
    height: 100%;
  }
 
  .el-submenu{
    text-align: left;
  }
 .iconfont{
   margin-right: 10px;
 }
 .el-menu{
border: none;
 }
</style>
